package com.smartplatform.controller;

import com.smartplatform.dto.R;
import com.smartplatform.pojo.SysCategory;
import com.smartplatform.service.SysCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 咕唧
 * @Date 2022/5/30 15:38
 * @Version 1.0
 */
@RestController
@RequestMapping("/categories")
public class SysCategoryController {

    private SysCategoryService sysCategoryService;

    @Autowired
    public void setSysCategoryService(SysCategoryService sysCategoryService) {
        this.sysCategoryService = sysCategoryService;
    }

    @PostMapping("/category")
    public R addCategory(@RequestBody SysCategory sysCategory) {
        sysCategoryService.addCategory(sysCategory);
        return R.setOK();
    }

    //RequestParam设置分页属性的默认参数
    @GetMapping("/categories")
    public R findByPage(int limit, int page) {
        return R.setOK(sysCategoryService.findAll(limit, page));
    }

    @DeleteMapping("/category/{cId}")
    public R deleteById(@PathVariable Long cId) {
        sysCategoryService.deleteByCId(cId);
        return R.setOK();
    }

    @DeleteMapping("/categories")
    public R deleteByIds(@RequestBody List<Long> ids,Long status) {
        sysCategoryService.deleteByIds(ids,status);
        return R.setOK();
    }

    @PostMapping("/categories")
    public R findByNameOrStatus(String categoryName,int status) {
        return R.setOK(sysCategoryService.findByNameOrStatus(categoryName,status));
    }

}
